//
//  YCSetViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/2/21.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCSetViewController.h"
#define YCSetCellIdentify @"YCSetTableViewCell"
#define YCSetBigCellIdentify @"YCSetBigTableViewCell"

@interface YCSetViewController ()
{
    UITableView *m_tableView;
    UIButton *m_btnLoginOut;
    
    NSString *service_tel;
}
@end
typedef enum {
    SECTION_CHEFSET,
    SECTION_PRINTSET,
    SECTION_PERSONSET,
    SECTION_COUNT
} SECTIONS;
@implementation YCSetViewController
-(void)viewWillAppear:(BOOL)animated
{
    [_naviVC setNaviHidden:NO];
    [_naviVC setMainNaviVC];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"设置";
    
    [self loadData];
    [self addTableView];
}
-(void)loadData
{
    SettingIndexRequest *request = [SettingIndexRequest new];
    [apiClient hideProgress];
    [apiClient doSettingIndex:request success:^(ApiResponse *data, NSString *url) {
        SettingIndexResponse *response=[[SettingIndexResponse alloc]initWithCGResponse:data];
        service_tel=response.service_tel;
        [m_tableView reloadData];
    }failure:^(ApiResponse *data, NSString *url){
    }];
}
-(void)addTableView
{
    m_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainScreen_Height-kNAV_HEIGHT)];
    m_tableView.delegate=self;
    m_tableView.dataSource=self;
    m_tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    m_tableView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:m_tableView];
    [m_tableView registerNib:[UINib nibWithNibName:YCSetCellIdentify bundle:nil] forCellReuseIdentifier:YCSetCellIdentify];
    [m_tableView registerNib:[UINib nibWithNibName:YCSetBigCellIdentify bundle:nil] forCellReuseIdentifier:YCSetBigCellIdentify];
    
    UIView *header=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, 5)];
    m_tableView.tableHeaderView=header;
    
    m_btnLoginOut=[[UIButton alloc]initWithFrame:CGRectMake(kMainScreen_Width*0.05, kMainScreen_Height-90-50, kMainScreen_Width*0.9, kCellHeight)];
    [m_btnLoginOut setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [m_btnLoginOut setTitle:@"退出登录" forState:UIControlStateNormal];
    [m_btnLoginOut.titleLabel setFont:[UIFont LightFontOfSize:18]];
    m_btnLoginOut.layer.borderWidth=1.0f;
    m_btnLoginOut.layer.borderColor=MAIN_COLOR.CGColor;
    m_btnLoginOut.layer.cornerRadius=4.0f;
    [m_btnLoginOut addTarget:self action:@selector(loginOut:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:m_btnLoginOut];
}
#pragma mark tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return SECTION_COUNT;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
#pragma mark tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==SECTION_PERSONSET) {
        return 110;
    }
    return 60.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    
    if (indexPath.section==SECTION_PERSONSET) {
        YCSetBigTableViewCell *_cell = (YCSetBigTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCSetBigCellIdentify forIndexPath:indexPath];
        
        [_cell.labDesc setText:service_tel];
        
        _cell.phoneBlock=^(){
            [self.view callPhoneWith:service_tel];
        };
        _cell.aboutBlock=^(){
            YCAboutViewController *aboutVC=[[YCAboutViewController alloc]init];
            [self showNavigationView:aboutVC];
        };
        cell=_cell;        
    }else{
        YCSetTableViewCell *_cell = (YCSetTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCSetCellIdentify forIndexPath:indexPath];
        NSArray *array=@[@"个人设置",@"打印机设置"];
        [_cell.labTitle setText:array[indexPath.section]];
        cell=_cell;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==SECTION_CHEFSET) {
        YCPersonnalSetViewController *setVC=[[YCPersonnalSetViewController alloc]init];
        [self showNavigationView:setVC];
    }
    if (indexPath.section==SECTION_PERSONSET) {
        
    }
    if (indexPath.section==SECTION_PRINTSET) {
        YCConnectViewController *printVC=[[YCConnectViewController alloc]init];
        [self showNavigationView:printVC];
    }
}
-(void)loginOut:(UIButton *)button
{
    UserLoginOutRequest *request = [UserLoginOutRequest new];
    [apiClient hideProgress];
    [apiClient doUserLoginOut:request success:^(ApiResponse *data, NSString *url) {
        [[App shared] setUser:nil];
        [[App shared]save];
        [[AppDelegate appDelegate]loginOut];
    }failure:^(ApiResponse *data, NSString *url){
    }];
}

@end











